package com.chen.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chen.entity.Orders;
import com.chen.entity.R;
import com.chen.entity.ShoppingCart;
import com.chen.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.util.List;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    @GetMapping("/page")
    public R page(int page, int pageSize, Long number, String beginTime, String endTime) throws ParseException {
        Page<Orders> p = ordersService.findPage(page, pageSize, number, beginTime, endTime);
        return R.success(p);
    }

    @GetMapping("/userPage")
    public R userPage(int page, int pageSize, HttpSession session) {
        Long userId = (Long) session.getAttribute("user");
        Page<Orders> p = ordersService.userPage(page, pageSize, userId);
        return R.success(p);
    }

    @PostMapping("/submit")
    public R submit(@RequestBody Orders orders) {
        ordersService.submit(orders);
        return R.success(null);
    }

    @PutMapping
    public R put(@RequestBody Orders orders){
        ordersService.updateById(orders);
        return R.success(null);
    }
    @PostMapping("/again")
    public R again(@RequestBody Orders ordes){
        List<ShoppingCart> again = ordersService.again(ordes);
        return R.success(again);
    }
}
